package com.czl.module_work.fragment

import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.PatrolBean
import com.czl.base.data.bean.TakeCareBean
import com.czl.module_work.BR
import com.czl.module_work.R
import com.czl.module_work.adapter.PatrolOrderAdapter
import com.czl.module_work.adapter.TakeCareOrderAdapter
import com.czl.module_work.adapter.TakeCareRecordAdapter
import com.czl.module_work.databinding.WorkFragmentPatrolOrderBinding
import com.czl.module_work.databinding.WorkFragmentPatrolResultBinding
import com.czl.module_work.view.FilterPop
import com.czl.module_work.viewModel.MyOrderDetailViewModel
import com.czl.module_work.viewModel.PatrolOrderViewModel
import com.czl.module_work.viewModel.PatrolRecordOrderViewModel
import com.czl.module_work.viewModel.TakeCareOrderViewModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition


/**
 * 创建日期：2021/12/27  13:59
 * 类说明:
 * @author：86152
 */
@Route(path = AppConstants.Router.Work.F_WORK_DEVICE_PATROL_RESULT_ORDER)
class PatrolResultOrderFragment :
    BaseFragment<WorkFragmentPatrolResultBinding, PatrolRecordOrderViewModel>() {

    private var type = MyOrderDetailViewModel.FILTER_ORDER_TYPE_TAKE_CARE

    private var searchKey = ""

    override fun useBaseLayout(): Boolean = false;

    private lateinit var mAdapter: PatrolOrderAdapter

    private lateinit var mTakeCareAdapter: TakeCareRecordAdapter


    override fun initContentView(): Int = R.layout.work_fragment_patrol_result

    override fun initVariableId(): Int = BR.viewModel

    override fun initData() {
        viewModel.tvTitle.set("查询结果")
        initAdapter()
        viewModel.setOrderType(type)
        viewModel.setKey(searchKey)
        viewModel.getOrders()
    }

    override fun initViewObservable() {
        viewModel.uc.clearOrderEvent.observe(this, {
            mAdapter.setNewInstance(mutableListOf())
        })
        viewModel.uc.loadOrderEvent.observe(this, { data ->
            binding.smartCommon.finishRefresh(true)
            binding.smartCommon.finishLoadMore()
            data ?: return@observe
            mAdapter.addData(data)
        })
        viewModel.uc.loadTakeCareOrderEvent.observe(this, { data ->
            binding.smartCommon.finishRefresh(true)
            binding.smartCommon.finishLoadMore()
            data ?: return@observe
            mTakeCareAdapter.addData(data)
        })
    }

    override fun initParam() {
        type = arguments?.getInt(
            AppConstants.BundleKey.WORK_ORDER_TYPE,
            MyOrderDetailViewModel.FILTER_ORDER_TYPE_TAKE_CARE
        ) ?: MyOrderDetailViewModel.FILTER_ORDER_TYPE_TAKE_CARE
        searchKey = arguments?.getString(AppConstants.BundleKey.WORK_ORDER_SEARCH_KEY) ?: ""
    }

    private fun initAdapter() {
        mAdapter = PatrolOrderAdapter()
        mTakeCareAdapter = TakeCareRecordAdapter()
        mTakeCareAdapter.setOnItemClickListener { adapter, view, position ->
            startContainerActivity(
                AppConstants.Router.Work.F_WORK_TAKE_CARE_ORDER_DETAIL,
                Bundle().apply {
                    putInt(
                        AppConstants.BundleKey.WORK_ORDER_STATUS,
                        MyOrderDetailViewModel.FILTER_ORDER_TYPE_WARNING
                    )
                    putInt(
                        AppConstants.BundleKey.WORK_ORDER_TYPE,
                        MyOrderDetailViewModel.FILTER_ORDER_TYPE_TAKE_CARE
                    )
                    putString(
                        AppConstants.BundleKey.WORK_ORDER_ID,
                        mTakeCareAdapter.data[position].orderId
                    )
                })
        }
        mAdapter.hideState(true)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            startContainerActivity(AppConstants.Router.Work.F_WORK_MY_ORDER_DETAIL, Bundle().apply {
                putInt(
                    AppConstants.BundleKey.WORK_ORDER_STATUS,
                    if (mAdapter.getItem(position).orderState.toInt() == 1 || mAdapter.getItem(
                            position
                        ).orderState.toInt() == 3
                    ) 3 else 3
                )
                putInt(
                    AppConstants.BundleKey.WORK_ORDER_TYPE,
                    MyOrderDetailViewModel.FILTER_ORDER_TYPE_CHECK
                )
                putInt(
                    AppConstants.BundleKey.WORK_ORDER_ID,
                    mAdapter.getItem(position).orderId.toInt()
                )
            })
        }
        binding.ryCommon.apply {
            adapter =
                if (type == MyOrderDetailViewModel.FILTER_ORDER_TYPE_TAKE_CARE) mTakeCareAdapter else mAdapter
        }
        val emptyView = View.inflate(context, R.layout.common_empty_layout_view, null)
        val titleView = emptyView.findViewById<TextView>(R.id.tv_empty)
        emptyView.findViewById<View>(R.id.ll_empty).setOnClickListener { v: View? -> binding.smartCommon.autoRefresh() }
        if(type == MyOrderDetailViewModel.FILTER_ORDER_TYPE_TAKE_CARE){
            titleView.text = "该设备暂无保养工单"
            mTakeCareAdapter.setEmptyView(emptyView)
        }else{
            titleView.text = "该设备暂无巡检工单"
            mAdapter.setEmptyView(emptyView)
        }
    }
}